package gw.com.android.ui.home;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gwtsz.gts2.hx.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import gw.com.android.presenter.http.JiepanRequest;
import gw.com.android.ui.views.GlideCircleTransform;
import gw.com.android.ui.views.WaveView;

/**
 * @author jett
 * @since 2017-11-22.
 */
public class HomeJiepanView extends RelativeLayout {

    private TextView title;
    private RelativeLayout rlLoading;
    private RelativeLayout rlContent;
    private LinearLayout llFailure;

    private ImageView icon;
    private TextView courseTitle;
    private TextView teacherName;
    private TextView position;
    private TextView state;
    private ImageView stateIcon;
    private WaveView stateWave;
    private TextView time;
    private TextView winrate;

    private String teacherId;

    public HomeJiepanView(Context context) {
        super(context);
    }

    public HomeJiepanView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HomeJiepanView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        title = (TextView) findViewById(R.id.jiepan_title_name);
        rlContent = (RelativeLayout) findViewById(R.id.rl_course);
        rlLoading = (RelativeLayout) findViewById(R.id.rl_loading);
        llFailure = (LinearLayout) findViewById(R.id.jiepan_failure);

        icon = (ImageView) findViewById(R.id.jiepan_icon);
        courseTitle = (TextView) findViewById(R.id.jiepan_course_title);
        teacherName = (TextView) findViewById(R.id.jiepan_teacher_name);
        position = (TextView) findViewById(R.id.jiepan_position);
        state = (TextView) findViewById(R.id.jiepan_state);
        stateIcon = (ImageView) findViewById(R.id.jiepan_state_icon);
        stateWave = (WaveView) findViewById(R.id.jiepan_state_wave);
        stateWave.setWaveColor(0xFFC5C5C5);
        time = (TextView) findViewById(R.id.jiepan_time);
        winrate = (TextView) findViewById(R.id.jiepan_winrate);

        ImageView loading = (ImageView) findViewById(R.id.iv_loading);
        RotateAnimation rotate = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5F, Animation.RELATIVE_TO_SELF, 0.5F);
        rotate.setRepeatCount(Animation.INFINITE);
        rotate.setDuration(500);
        rotate.setFillAfter(true);
        rotate.setInterpolator(new LinearInterpolator());
        rotate.setStartOffset(0);
        loading.startAnimation(rotate);
    }

    public void setTitle(String title) {
        this.title.setText(title);
    }

    public void refreshCourse(JiepanRequest.CourseData course) {
        rlLoading.setVisibility(GONE);
        if (course == null) {
            if (TextUtils.isEmpty(teacherId)) {
                llFailure.setVisibility(VISIBLE);
                rlContent.setVisibility(GONE);
            }
        } else {
            teacherId = course.lecturerId;
            llFailure.setVisibility(GONE);
            rlContent.setVisibility(VISIBLE);
            courseTitle.setText(course.title);
            teacherName.setText(course.lecturer);
            if (!course.isNext) {
                stateIcon.setVisibility(GONE);
                stateWave.setVisibility(VISIBLE);
                winrate.setVisibility(VISIBLE);
                state.setText(R.string.home_jiepan_playing);
                String time = course.startTime + "-" + course.endTime;
                this.time.setText(time);
            } else {
                stateIcon.setVisibility(VISIBLE);
                stateWave.setVisibility(GONE);
                winrate.setVisibility(GONE);
                state.setText(R.string.home_jiepan_next);
                String time = getFormatDate(course.date, course.startTime);
                this.time.setText(time);
                stateIcon.setImageResource(R.mipmap.a_home_video_next);
            }
        }
    }

    public void refreshTeacher(JiepanRequest.TeacherData teacher) {
        if (teacher != null && !TextUtils.isEmpty(teacher.userNo)) {
            if (teacher.userNo.equals(teacherId)) {
                if (!TextUtils.isEmpty(teacher.winRate)) {
                    String text = String.format(Locale.getDefault(), getResources().getString(R.string.home_jiepan_winrate), teacher.winRate);
                    winrate.setText(text);
                }
                position.setText(teacher.position);
                Drawable drawable = icon.getDrawable();
                Glide.with(getContext()).load(teacher.avatar).placeholder(drawable).error(drawable).transform(new GlideCircleTransform(getContext())).into(icon);
            }
        }
    }

    public void refreshFailure() {
        if (TextUtils.isEmpty(teacherId)) {
            rlLoading.setVisibility(GONE);
            llFailure.setVisibility(VISIBLE);
            rlContent.setVisibility(GONE);
        }
    }

    private String getFormatDate(String date, String time) {
        String formatDate = null;
        try {
            long millions = Long.parseLong(date);
            formatDate = new SimpleDateFormat("yyyy.MM.dd", Locale.getDefault()).format(new Date(millions));
        } catch (Throwable ignore) {
        }
        if (TextUtils.isEmpty(formatDate)) {
            if (TextUtils.isEmpty(time)) {
                return "";
            } else {
                return time;
            }
        } else {
            if (TextUtils.isEmpty(time)) {
                return formatDate;
            } else {
                return formatDate + " " + time;
            }
        }
    }

}
